#include "TabDrawer.h"
#include "DockTab.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QPainter>
#include <QStyleOption>

TabDrawer::TabDrawer(const QString& title, QWidget* parent)
    : QWidget(parent)
    , m_title(title)
    , m_content(nullptr)
    , m_associatedTab(nullptr)
    , m_mainLayout(nullptr)
    , m_titleButton(nullptr)
    , m_contentWidget(nullptr)
    , m_contentLayout(nullptr)
    , m_isExpanded(false)
    , m_expandedHeight(DEFAULT_CONTENT_HEIGHT)
    , m_animation(nullptr)
{
    setupDrawer();
}

TabDrawer::TabDrawer(DockTab* tab, QWidget* parent)
    : QWidget(parent)
    , m_title(tab ? tab->tabTitle() : QString())
    , m_icon(tab ? tab->tabIcon() : QIcon())
    , m_content(nullptr)
    , m_associatedTab(tab)
    , m_mainLayout(nullptr)
    , m_titleButton(nullptr)
    , m_contentWidget(nullptr)
    , m_contentLayout(nullptr)
    , m_isExpanded(false)
    , m_expandedHeight(DEFAULT_CONTENT_HEIGHT)
    , m_animation(nullptr)
{
    if (tab)
    {
        m_content = tab->contentWidget();
    }
    setupDrawer();
}

TabDrawer::~TabDrawer()
{
    if (m_animation)
    {
        m_animation->stop();
    }
}

void TabDrawer::setupDrawer()
{
    // 创建主布局
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setContentsMargins(0, 0, 0, 0);
    m_mainLayout->setSpacing(0);
    
    // 创建标题按钮
    m_titleButton = new QPushButton(this);
    m_titleButton->setFixedHeight(TITLE_HEIGHT);
    m_titleButton->setFlat(true);
    m_titleButton->setCheckable(true);
    updateTitleButton();
    m_mainLayout->addWidget(m_titleButton);
    
    // 创建内容容器
    m_contentWidget = new QWidget(this);
    m_contentWidget->setFixedHeight(0); // 初始折叠状态
    m_contentLayout = new QVBoxLayout(m_contentWidget);
    m_contentLayout->setContentsMargins(4, 4, 4, 4);
    m_contentLayout->setSpacing(0);
    m_mainLayout->addWidget(m_contentWidget);
    
    // 如果有内容，添加到布局
    if (m_content)
    {
        m_contentLayout->addWidget(m_content);
    }
    
    // 创建动画
    m_animation = new QPropertyAnimation(this, "contentHeight", this);
    m_animation->setDuration(200);
    m_animation->setEasingCurve(QEasingCurve::OutQuad);
    
    // 连接信号
    connect(m_titleButton, &QPushButton::clicked, this, &TabDrawer::onTitleButtonClicked);
    connect(m_animation, &QPropertyAnimation::finished, this, &TabDrawer::onAnimationFinished);
    
    // 样式由QSS控制
}

// === 基础属性 ===

void TabDrawer::setTitle(const QString& title)
{
    if (m_title != title)
    {
        m_title = title;
        updateTitleButton();
    }
}

void TabDrawer::setIcon(const QIcon& icon) {
    if (m_icon.cacheKey() != icon.cacheKey()) {
        m_icon = icon;
        updateTitleButton();
    }
}

// === 内容管理 ===

void TabDrawer::setContent(QWidget* content) {
    if (m_content == content) return;
    
    // 移除旧内容
    if (m_content && m_contentLayout) {
        m_contentLayout->removeWidget(m_content);
        m_content->setParent(nullptr);
    }
    
    m_content = content;
    
    // 添加新内容
    if (m_content && m_contentLayout) {
        m_content->setParent(m_contentWidget);
        m_contentLayout->addWidget(m_content);
    }
    
    emit contentChanged(content);
}

void TabDrawer::setAssociatedTab(DockTab* tab) {
    m_associatedTab = tab;
    
    if (tab) {
        setTitle(tab->tabTitle());
        setIcon(tab->tabIcon());
        setContent(tab->contentWidget());
    }
}

// === 展开/折叠 ===

void TabDrawer::setExpanded(bool expanded, bool animated) {
    if (m_isExpanded == expanded) return;
    
    m_isExpanded = expanded;
    m_titleButton->setChecked(expanded);
    
    if (animated && m_animation) {
        int fromHeight = m_contentWidget->height();
        int toHeight = expanded ? m_expandedHeight : 0;
        startAnimation(fromHeight, toHeight);
    } else {
        setContentHeight(expanded ? m_expandedHeight : 0);
        emit toggled(expanded);
    }
}

// === 动画属性 ===

int TabDrawer::contentHeight() const {
    return m_contentWidget ? m_contentWidget->height() : 0;
}

void TabDrawer::setContentHeight(int height) {
    if (m_contentWidget) {
        m_contentWidget->setFixedHeight(height);
    }
}

// === Qt重写 ===

void TabDrawer::paintEvent(QPaintEvent* event) {
    // 使用样式表绘制，无需自定义绘制
    QWidget::paintEvent(event);
}

QSize TabDrawer::sizeHint() const {
    int height = TITLE_HEIGHT;
    if (m_isExpanded) {
        height += m_expandedHeight;
    }
    return QSize(250, height);
}

QSize TabDrawer::minimumSizeHint() const {
    return QSize(40, TITLE_HEIGHT);
}

// === 槽函数 ===

void TabDrawer::onTitleButtonClicked() {
    toggle();
    emit titleClicked();
}

void TabDrawer::onAnimationFinished() {
    emit toggled(m_isExpanded);
}

// === 私有方法 ===

void TabDrawer::updateTitleButton() {
    if (!m_titleButton) return;
    
    // 设置文本和图标
    m_titleButton->setText(m_title);
    if (!m_icon.isNull()) {
        m_titleButton->setIcon(m_icon);
    }
    
    // 设置展开/折叠指示器
    QString arrowText = m_isExpanded ? "▼ " : "▶ ";
    m_titleButton->setText(arrowText + m_title);
}

void TabDrawer::startAnimation(int fromHeight, int toHeight) {
    if (!m_animation) return;
    
    m_animation->stop();
    m_animation->setStartValue(fromHeight);
    m_animation->setEndValue(toHeight);
    m_animation->start();
}
